/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package getrootbahasa;

/**
 *
 * @author WaOnEmperoR
 */
import com.mysql.jdbc.PreparedStatement;
import com.stemaini.core.doc.Dokumen;
import com.stemaini.core.doc.Terms;
import com.stemaini.core.doc.Vektor;
import com.stemaini.core.database.Kamus;
import com.stemaini.core.doc.VektorNode;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import taini.koneksi.KoneksiAini;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.misc.SerializedClassifier;
import weka.core.Debug;
import weka.core.Instances;
import weka.core.converters.ArffLoader;

public class MainV5_GenARFF_Testing {
    public static void main(String[] args) throws FileNotFoundException, IOException{
        KoneksiAini theKoneksi = new KoneksiAini();

        String isi = "";
        String prepare = "";

        try {
            theKoneksi.connectFirst();
            String sql = "Select Word from Word_IDF order by Word";
            ResultSet res = theKoneksi.executeSelect(sql);
            while (res.next()){
                isi=(res.getString(1));
                prepare = prepare + isi + ",";

                System.out.println(isi);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        prepare = prepare + "class_kat\n";

        try {
            //Pengulangan menurut nomor dokumen
            //Lebih bagus jika menggunakan Multithreading (di atas dokumen ke-50 proses komputasi melambat)
            //Nyoh
            theKoneksi.connectFirst();
            for (int j=1; j<=18; j++){
                String sql = "Select Word from Word_IDF order by Word";
                ResultSet res = theKoneksi.executeSelect(sql);

                System.out.println("Dokumen ke : " + j);

                while (res.next()){
                    isi=(res.getString(1));
                    String sql2 = "select count(*) as Hasil from word_frec_no_testing where Kata = '" + isi + "' and Nomor_Dokumen = " + j;
                    ResultSet res2 = theKoneksi.executeSelect(sql2);


                    int jumlah = 0;
                    while (res2.next()){
                        jumlah = Integer.parseInt(res2.getString(1));
                    }

                    //Kalau dokumen mengandung kata yang dihitung IDF-nya
                    if (jumlah>0){
                        String sql3 = "select TF_IDF from word_frec_no_testing where Kata = '" + isi + "' and Nomor_Dokumen = " + j;
                        //System.out.println(sql3);
                        ResultSet res3 = theKoneksi.executeSelect(sql3);


                        double jumlah2 = 0.00;

                        while (res3.next()){
                            jumlah2 = Double.parseDouble(res3.getString(1));
                        }

                        prepare = prepare + jumlah2 + ",";
                    }
                    else{
                        prepare = prepare + "0,";
                    }

                }

                prepare = prepare + "?\n";
            }

            try {
                FileWriter fstream = new FileWriter("test_TFIDF.csv");
                BufferedWriter out = new BufferedWriter(fstream);
                out.write(prepare);
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }

            CSVtoArff konv = new CSVtoArff("test_TFIDF.csv", "test_TFIDF.arff");

            konv.Convert();
/*
            BufferedReader in = new BufferedReader(new FileReader("test_TFIDF.arff"));
            int j = 1;
            String line;
            while((line = in.readLine()) != null)
            {
                if(line.contains("class_kat"))
                {
                    line.replaceAll("string", "{Agama,Ekonomi,Infotainment,Olahraga,Politik,Teknologi,Budaya,Hukum,Kesehatan}");
                }
                j++;
            }
            in.close();
 *
 */
        BufferedReader in = new BufferedReader(new FileReader("test_TFIDF.arff"));
        PrintWriter writer =  new PrintWriter(new BufferedWriter(new FileWriter("test_TFIDF1.arff")));
        String line;

        while((line = in.readLine()) != null)
        {
            System.out.println("tes");
            if(line.contains("class_kat"))
            {
               // line.replace(oldChar, newChar)
                 line = "@attribute class_kat {Agama,Ekonomi,Infotainment,Olahraga,Politik,Teknologi,Budaya,Hukum,Kesehatan}";
                 System.out.println(line);
            }

            writer.println(line);
        }

      /*  File realName = new File("test_TFIDF.arff");
        realName.delete(); // remove the old file
        new File("test_TFIDF.arff").renameTo(realName);*/

        writer.close();
        in.close();
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
